package com.iretailer.report.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.mapper.PageWidgetsMapper;
import com.iretailer.report.mapper.PageWidgetsUserMapper;
import com.iretailer.report.model.PageWidgets;
import com.iretailer.report.model.PageWidgetsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 13:04
 * @Description:
 */
@Service
public class PageWidgetService extends ServiceImpl<PageWidgetsMapper, PageWidgets> {

    @Autowired
    private PageWidgetsMapper pageWidgetsMapper;
    @Autowired
    private PageWidgetsUserService pageWidgetsUserService;


    public boolean deleteByIds(List<Long> ids) {
        Integer result = pageWidgetsMapper.deleteBatchIds(ids);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<PageWidgets> selectByIds(List<String> ids) {
        return pageWidgetsMapper.selectByIds(ids);
    }

    public boolean saveOrUpdate(PageWidgets pageWidgets) {
        if (pageWidgets.getId() != null && pageWidgets.getId() > 0) {
            pageWidgetsMapper.updateById(pageWidgets);
            return true;
        } else {
            pageWidgetsMapper.insert(pageWidgets);
            return true;
        }
    }

    public List<PageWidgets> selectAll() {
        return pageWidgetsMapper.selectList(null);
    }

    public List<PageWidgets> selectPageWidgetsByUserId(String userId) {

        List<PageWidgets> list = pageWidgetsMapper.selectPageWidgetsByUserId(userId);
        list.forEach(it -> {
            it.setOptions(it.getOptions());
        });

        return list;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean replacePageWidgets_users(List<PageWidgets> pageWidgets, String userId) {
        pageWidgetsMapper.deletePageWidgets_usersByUserId(userId);
        List<PageWidgetsUser> pos = new ArrayList<>();
        pageWidgets.forEach(it -> {
            PageWidgetsUser pwu = new PageWidgetsUser();
            pwu.setUserId(userId);
            pwu.setWidgetId(it.getId());
            pwu.setH(it.getH());
            pwu.setW(it.getW());
            pwu.setX(it.getX());
            pwu.setY(it.getY());
            pwu.setOptions(it.getOptions());
            pos.add(pwu);
        });
        pageWidgetsUserService.remove(new QueryWrapper<PageWidgetsUser>().lambda().eq(PageWidgetsUser::getUserId, userId));
        if (pos.size() > 0) {
            pageWidgetsUserService.replacePageWidgets_users(pos);
            return true;
        }
        return true;

    }


    public int deletePageWidgets_usersByUserId(String userId) {
        return pageWidgetsMapper.deletePageWidgets_usersByUserId(userId);
    }
}
